SRCDIRS	:=./
			
INCDIRS :=./

TARGET := httpserver

OUTPUT_DIR_NAME := obj
cross_complier =
complier := gcc
cpp_complier:= g++
LD := ld
OBJCOPY := objcopy
CFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))
CPLUSPLUSFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.cpp))

SFILES :=$(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))

CSOUCEFILE_NODIR :=$(notdir $(CFILES))
SSOUCEFILE_NODIR :=$(notdir $(SFILES))
CPLUSPLUSSOUCEFILE_NODIR :=$(notdir $(CPLUSPLUSFILES))


COBJS :=$(CSOUCEFILE_NODIR:%.c=$(OUTPUT_DIR_NAME)/%.o)
CPLUSPLUSOBJS :=$(CPLUSPLUSSOUCEFILE_NODIR:%.cpp=$(OUTPUT_DIR_NAME)/%.o)
SOBJS :=$(SSOUCEFILE_NODIR:%.S=$(OUTPUT_DIR_NAME)/%.o)

OBJS := $(COBJS) $(SOBJS) $(CPLUSPLUSOBJS)
INCLUDE			:= $(patsubst %, -I %, $(INCDIRS))

C_DEFINES:=-D__HELLO__

CFLAGS :=$(C_DEFINES) -Wall -std=c11 -O2
CPPFLAGS :=$(C_DEFINES) -Wall -std=c++11 -O2 
LINKLIB= -levent
VPATH			:= $(SRCDIRS)
.PHONY:clean rebuild  debug
#$(TARGET).bin:$(OBJS)
all:createDirs  $(TARGET)

$(TARGET):$(OBJS)
	#$(LD) -Timx6ul.lds -o $(OUTPUT_DIR_NAME)/$(TARGET).elf  $^
	#$(OBJCOPY) -O binary -S $(OUTPUT_DIR_NAME)/$(TARGET).elf $@
	$(cpp_complier) -o $(OUTPUT_DIR_NAME)/$(TARGET) $^ $(LINKLIB)

$(OUTPUT_DIR_NAME)/%.o:%.c
	$(complier)	$(CFLAGS) $(INCLUDE) -c  $<  -o $@ 
$(OUTPUT_DIR_NAME)/%.o:%.S
	$(complier)  $(CFLAGS) $(INCLUDE)	-c $<   -o $@ 
$(OUTPUT_DIR_NAME)/%.o:%.cpp
	$(cpp_complier)  $(CPPFLAGS) $(INCLUDE)	-c $<  -o $@ 

createDirs:
	@mkdir -p $(OUTPUT_DIR_NAME)
		
clean:
	rm -rf $(OUTPUT_DIR_NAME)/*.o $(OUTPUT_DIR_NAME)/*.out  $(TARGET)* $(OUTPUT_DIR_NAME)/$(TARGET)*
	
rebuild:clean $(TARGET)

debug:
	@echo $(CFILES)
	@echo $(SFILES)
	@echo $(CPLUSPLUSFILES)
	@echo $(CSOUCEFILE_NODIR)
	@echo $(SSOUCEFILE_NODIR)
	@echo $(CPLUSPLUSSOUCEFILE_NODIR)
	@echo $(OBJS)